//
//  Detail_MapVC.m
//  HappyRent
//
//  Created by CoderYee on 2017/4/6.
//  Copyright © 2017年 内圈科技. All rights reserved.
//

#import "Detail_MapVC.h"
#import "Detail_MapSearchVC.h"  //地图SearchVC
@interface Detail_MapVC ()<BMKMapViewDelegate,BMKLocationServiceDelegate,UISearchBarDelegate,BMKGeoCodeSearchDelegate>
{
    BMKMapView          *_mapView;
    BMKLocationService  *_locService;
    BMKGeoCodeSearch    *_geocodesearch;
    NSString            *_city;
    
}
@end
//地图展示,定位,获取触摸点
@implementation Detail_MapVC

-(void)dealloc{
   
    _mapView=nil;
    _mapView.delegate=nil;
    _locService=nil;
    _locService.delegate=nil;
    _geocodesearch=nil;
    _geocodesearch.delegate=nil;
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
    _mapView.delegate=nil;
    [_locService stopUserLocationService];
    _locService=nil;
}
-(void)addNavbarView{
    
    [super addNavbarView];
    
    [self addSearchBar];
    
}
-(void)configOwnViews{
    
    [super configOwnViews];
}
-(void)addOwnViews{
    
    [super addOwnViews];
    
    [self addMapView];
    
    [self configurelocService];
    
    [self  addCenterView];
}
#pragma mark 添加地图
-(void)addMapView{
    
    _mapView=[[BMKMapView alloc] initWithFrame:self.view.bounds];
    _mapView.delegate=self;
    _mapView.mapType=BMKMapTypeStandard;
    _mapView.showMapScaleBar=YES;
    _mapView.zoomLevel=18;
    _mapView.userTrackingMode=BMKUserTrackingModeFollow;
    _mapView.trafficEnabled=YES;
    _mapView.showsUserLocation=YES;//显示用户的当前位置
    [self.view addSubview:_mapView];
}
#pragma mark 配置定位
-(void)configurelocService{

    _locService=[[BMKLocationService alloc] init];
    _locService.distanceFilter=kCLLocationAccuracyBest;
    _locService.delegate=self;
    [_locService startUserLocationService];//开始定位
}
-(void)addCenterView{
    
    UIImageView  *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 35)];
    [imageView setImage:[UIImage imageNamed:@"datouzhen"]];
    imageView.contentMode=UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds=YES;
    imageView.center=self.view.center;
    [self.view addSubview:imageView];
}
- (void)addSearchBar
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,IPhone4_5_6_6P(200, 220, 250, 280), 30)];
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    searchBar.frame = CGRectMake(0, 0,IPhone4_5_6_6P(200, 220, 250, 280), 30);
    searchBar.layer.cornerRadius = 15;
    searchBar.layer.masksToBounds = YES;
    [searchBar.layer setBorderWidth:8];
    [searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];  //设置边框为白色
    searchBar.placeholder = @"请搜索你想要的东西";
    [titleView addSubview:searchBar];
    [self.navigationItem.titleView sizeToFit];
    self.navigationItem.titleView = titleView;
}
#pragma mark  BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    
    [_mapView updateLocationData:userLocation];
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    _mapView.showsUserLocation=YES;
    [_locService stopUserLocationService];
    [self doEventReverseGeocode:userLocation.location.coordinate];//获取当前城市
    
}
//定位失败
- (void)didFailToLocateUserWithError:(NSError *)error{
    
    
    
}
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
  
    
    
}
#pragma mark   根据当前用户的位置解析出当前城市
-(void)doEventReverseGeocode:(CLLocationCoordinate2D )coordinate{
   
    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    _geocodesearch.delegate=self;
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = coordinate;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
    }
    else
    {
        NSLog(@"反geo检索发送失败");
    }
}
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_OPEN_NO_ERROR)
    {
        _city=result.addressDetail.city;
    }
}
//根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    NSString *AnnotationViewID = @"annotationViewID";
    //根据指定标识查找一个可被复用的标注View，一般在delegate中使用，用此函数来代替新申请一个View
    BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil) {
        
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        
        }
    
    ((BMKPinAnnotationView*)annotationView).animatesDrop = NO;
    annotationView.canShowCallout=NO;
    annotationView.image=[UIImage imageNamed:@"datouzhen"];
    
    annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
    annotationView.annotation = annotation;
    annotationView.canShowCallout = YES;
    return annotationView;
}

- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate{
//这里需要判断是否定位成功，获取到位置
//    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
//    annotation.coordinate = coordinate;
//    annotation.title=@"昌平";
//    [_mapView addAnnotation:annotation];
    [_mapView setCenterCoordinate:coordinate animated:YES];

}
#pragma mark UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   
    
    
    
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    
    //用于城市检索
    if (_city.length==0) {//查询城市未成功，不允许跳转界面
        
        return;
    }
    Detail_MapSearchVC *searchVc=[Detail_MapSearchVC new];
    searchVc.currentCity=_city;
    [self.navigationController presentViewController:searchVc animated:YES completion:^{
        
    }];
    
}

@end
